using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class DisplayRound : MonoBehaviour {
	
	public GameRessource gameRessource;
	public TextAsset xmlPersonnage;
	public TextAsset xmlSpells;
	
	public Dictionary<int, Spells> ListSpellJ1;
	public Dictionary<int, Spells> ListSpellJ2;
	public Dictionary<int, Spells> EffectSpellJ1;
	public Dictionary<int, Spells> EffectSpellJ2;
	public Personnage J1;
	public Personnage J2;
	
	/****************************************************************************
	*****************************************************************************
											HUD
	*****************************************************************************
	*****************************************************************************/
	public CCText NumeroTour;
	//La vie des 2 joueurs
		public CCText ChiffreVieJ1;
		public CCText ChiffreVieJ2;
		public CCText MaxVieJ1;
		public CCText MaxVieJ2;
		//barre de vie
		public Transform BarreVieJ1;
		public Transform BarreVieJ2;
	//
	//Ressource des 2 joueurs
		public CCText ChiffreRessourceJ1;
		public CCText ChiffreRessourceJ2;
		public CCText MaxRessourceJ1;
		public CCText MaxRessourceJ2;
		//barre de ressource
		public Transform BarreRessourceJ1;
		public Transform BarreRessourceJ2;
	//
	//Barriere des 2 joueurs
		public CCText ChiffreBarriereJ1;
		public CCText ChiffreBarriereJ2;
		public CCText MaxBarriereJ1;
		public CCText MaxBarriereJ2;
	
		//barre de barriere
		public Transform BarreBarriereJ1;
		public Transform BarreBarriereJ2;
	//
	//Les Hud d'effet visuel comme les flashs et icone de jeu
		public MeshRenderer FlashJ1;
		public MeshRenderer FlashJ2;
		public MeshRenderer IconeSpellJ1;
		public MeshRenderer IconeSpellJ2;
		public MeshRenderer IconeSpellJ1Cross;
		public MeshRenderer IconeSpellJ2Cross;
	//
	//Les chiffres qui grossissent pour montrer les degats et les perte de ressource
		public CCText GainPerteRessourceJ1;
		public CCText GainPerteRessourceJ2;
		public CCText GainPerteBarriereJ1;
		public CCText GainPerteBarriereJ2;
		public CCText GainVieJ1;
		public CCText GainVieJ2;
		public CCText PerteVieJ1;
		public CCText PerteVieJ2;
	/****************************************************************************
	*****************************************************************************
										FIN	HUD
	*****************************************************************************
	*****************************************************************************/
	
	//Constructeur
	//
	public void setDisplayRound(Dictionary<int, Spells> ListSpellPerso1, Dictionary<int, Spells> ListSpellPerso2, Dictionary<int, Spells> CreateEffectSpell1, Dictionary<int, Spells> CreateEffectSpell2, Personnage Perso1, Personnage Perso2){
		this.ListSpellJ1 = ListSpellPerso1;
		this.ListSpellJ2 = ListSpellPerso2;
		this.EffectSpellJ1 = CreateEffectSpell1;
		this.EffectSpellJ2 = CreateEffectSpell2;
		this.J1 = Perso1;
		this.J2 = Perso2;
	}
	public void Clean(){
		IconeSpellJ1.gameObject.SetActiveRecursively(false);
		IconeSpellJ2.gameObject.SetActiveRecursively(false);
		IconeSpellJ1Cross.gameObject.SetActiveRecursively(false);
		IconeSpellJ2Cross.gameObject.SetActiveRecursively(false);
		FlashJ1.gameObject.SetActiveRecursively(false);
		FlashJ2.gameObject.SetActiveRecursively(false);
		
		GainVieJ1.gameObject.SetActiveRecursively(false);
		GainVieJ2.gameObject.SetActiveRecursively(false);
		PerteVieJ1.gameObject.SetActiveRecursively(false);
		PerteVieJ2.gameObject.SetActiveRecursively(false);
		GainPerteRessourceJ1.gameObject.SetActiveRecursively(false);
		GainPerteRessourceJ2.gameObject.SetActiveRecursively(false);
		GainPerteBarriereJ1.gameObject.SetActiveRecursively(false);
		GainPerteBarriereJ2.gameObject.SetActiveRecursively(false);
		
		//Mettre la bonne taille au hud
		GainVieJ1.transform.localScale = new Vector3(0.45f,0.45f,0.45f);
		GainVieJ2.transform.localScale = new Vector3(0.45f,0.45f,0.45f);
		PerteVieJ1.transform.localScale = new Vector3(0.45f,0.45f,0.45f);
		PerteVieJ2.transform.localScale = new Vector3(0.45f,0.45f,0.45f);
		GainPerteRessourceJ1.transform.localScale = new Vector3(0.45f,0.45f,0.45f);
		GainPerteRessourceJ2.transform.localScale = new Vector3(0.45f,0.45f,0.45f);
		GainPerteBarriereJ1.transform.localScale = new Vector3(0.45f,0.45f,0.45f);
		GainPerteBarriereJ2.transform.localScale = new Vector3(0.45f,0.45f,0.45f);
	}
	
	void Awake(){
		gameRessource = new GameRessource(xmlPersonnage,xmlSpells);
	}
	public void ShowSpellIcon(int NbRound){
		NbRound = NbRound-1;
		if(J1.CurrentSpell!=null){
			IconeSpellJ1.gameObject.SetActiveRecursively(true);
			IconeSpellJ1.material.mainTexture = J1.CurrentSpell.Image;
			if(J1.CurrentSpell.IsInterrupt){
				IconeSpellJ1Cross.gameObject.SetActiveRecursively(true);
			}
			else
				IconeSpellJ1Cross.gameObject.SetActiveRecursively(false);
		}
		if(J2.CurrentSpell!=null){
			IconeSpellJ2.gameObject.SetActiveRecursively(true);
			IconeSpellJ2.material.mainTexture = J2.CurrentSpell.Image;
			if(J2.CurrentSpell.IsInterrupt){
				IconeSpellJ2Cross.gameObject.SetActiveRecursively(true);
			}
			else
				IconeSpellJ2Cross.gameObject.SetActiveRecursively(false);
		}
		if(EffectSpellJ1.ContainsKey(NbRound)){
			IconeSpellJ1.material.color = Color.white;
		}
		else{
			IconeSpellJ1.material.color = Color.grey;
		}
		
		if(EffectSpellJ2.ContainsKey(NbRound)){
			IconeSpellJ2.material.color = Color.white;
		}
		else{
			IconeSpellJ2.material.color = Color.grey;
		}
	}
	public IEnumerator Damage(int NbRound, Dictionary<int, Spells> EffectSpellJ,MeshRenderer IconeSpellJ,MeshRenderer IconeSpellJCross,CCText PerteVieAdv,CCText ChiffreBarriere,MeshRenderer FlashAdv,CCText VieAdv,CCText BarriereAdv,CCText RessourceJ,CCText GainPerteRessource ,Personnage J1,Personnage Adv){
		NbRound = NbRound-1;
		if(EffectSpellJ.ContainsKey(NbRound) && EffectSpellJ[NbRound].Instant != 2){
			Spells SpellPerso = new Spells(EffectSpellJ[NbRound]);
			if(SpellPerso.Type=="Interrupt" ||SpellPerso.Type=="Attaque"){
				if(J1.currentSpell != null){
					if(!J1.currentSpell.IsInterrupt){
						yield return StartCoroutine(EffectHit(SpellPerso, IconeSpellJ, PerteVieAdv,ChiffreBarriere,FlashAdv,VieAdv,BarriereAdv,RessourceJ,GainPerteRessource,Adv,J1));
					}
					else{
						//IconeSpellJCross.gameObject.SetActiveRecursively(false);
						//IconeSpellJ.gameObject.SetActiveRecursively(false);
					}
				}
			}
		}
		yield return new WaitForSeconds(0.5f);
	}
	public IEnumerator Heal(int NbRound,Dictionary<int, Spells> EffectSpellJ,MeshRenderer IconeSpellJ,CCText GainVieJ,CCText GainBarriereJ,MeshRenderer FlashJ,CCText VieJ,CCText BarriereJ,CCText RessourceJ,CCText GainPerteRessourceJ,Personnage J){
	//Meme si les soins sont instant on fait quand meme lautre cas pour etre sur que sa foire pas si on modifie ou rajoute
		NbRound = NbRound-1;
		if(EffectSpellJ.ContainsKey(NbRound)){
			Spells SpellPerso = new Spells(EffectSpellJ[NbRound]);
			if(SpellPerso.Type=="Defense"){
				//Defense D'acier : Warrior  ==> Pas de code qui sert voir dans effect hit pour le code concernant ce sort
				//
				if(SpellPerso.IdSpell == 103){
					if(!J.currentSpell.IsInterrupt)
						Debug.Log(" ");
						//yield return StartCoroutine(EffectHeal(SpellPerso,IconeSpellJ,GainVieJ,FlashJ,VieJ,RessourceJ,GainPerteRessourceJ,J));
					else{
						//IconeSpellJ1Cross.gameObject.SetActiveRecursively(false);
						//IconeSpellJ1.gameObject.SetActiveRecursively(false);
					}
				}
				//Barriere : Wizard
				//
				if(SpellPerso.IdSpell == 204){
					if(!J.currentSpell.IsInterrupt)
						yield return StartCoroutine(EffectBoubou(SpellPerso,IconeSpellJ,GainBarriereJ,FlashJ,BarriereJ,RessourceJ,GainPerteRessourceJ,J));
					else{
						//IconeSpellJ1Cross.gameObject.SetActiveRecursively(false);
						//IconeSpellJ1.gameObject.SetActiveRecursively(false);
					}
				}
				//Guerison : Thief
				//
				if(SpellPerso.IdSpell == 305){
					if(!J.currentSpell.IsInterrupt)
						yield return StartCoroutine(EffectHeal(SpellPerso,IconeSpellJ,GainVieJ,FlashJ,VieJ,RessourceJ,GainPerteRessourceJ,J));
					else{
						//IconeSpellJ1Cross.gameObject.SetActiveRecursively(false);
						//IconeSpellJ1.gameObject.SetActiveRecursively(false);
					}
				}
			}
		}
		yield return new WaitForSeconds(0.5f);
	}
	public IEnumerator EffectHit(Spells spell, MeshRenderer ImageIcone, CCText ChiffreDommage,CCText ChiffreBarriere, MeshRenderer ZoneFlash, CCText HudVie,CCText HudBarriere, CCText HudRessource,CCText GainPerteRessource, Personnage PersoReceveur, Personnage PersoAttaquant){
		ImageIcone.gameObject.SetActiveRecursively(true);
		ImageIcone.material.mainTexture = spell.Image;
		int damageSpell = int.Parse(spell.Damage.ToString());
		CCText HudGainPerteRessourceAdv;
		CCText HudRessourceAdv;
		//Si l'adversaire est en défense d'acier
		//
		if(PersoReceveur.currentSpell.IdSpell==103){
			if(HudRessource.name=="ChiffreRessourceJ1"){
				HudGainPerteRessourceAdv=this.GainPerteRessourceJ2;
				HudRessourceAdv=this.ChiffreRessourceJ2;
			}else{
				HudGainPerteRessourceAdv=this.GainPerteRessourceJ2;
				HudRessourceAdv=this.ChiffreRessourceJ1;
			}	
			HudGainPerteRessourceAdv.Text="+"+damageSpell;
			HudGainPerteRessourceAdv.gameObject.SetActiveRecursively(true);
			StartCoroutine(ScaleText(HudGainPerteRessourceAdv));
			HudRessourceAdv.Text = PersoReceveur.NbRessource.ToString();
			damageSpell = damageSpell /2;
		}

		//Ici bug avec ces attaques là, le chiffre du shield wizard devrai indiquer -5, hors il ne le fait pas
		//car lorsque l'on arrive dans la boucle shield du wizard deja égale a 0 et donc ne fait pas le -5
		/*
		 * 
		 * ListSpellPerso1.Add(0,204);
			ListSpellPerso1.Add(2,203);
			ListSpellPerso1.Add(4,203);
			ListSpellPerso1.Add(6,203);
			ListSpellPerso1.Add(8,203);
		
			ListSpellPerso2.Add(0,104);
			ListSpellPerso2.Add(2,104);
			ListSpellPerso2.Add(4,104);
			ListSpellPerso2.Add(6,104);
			ListSpellPerso2.Add(8,104);
		 * 
		 * */
		//Et lorsque l'on met le boubou et que l'autre attaque en meme temps, cela affiche pas le bon chiffre.
		//
		//Debug.LogError("PersoReceveur.Shield = "+PersoReceveur.Shield);
		if(PersoReceveur.Shield>0){
			if(int.Parse(ChiffreBarriere.Text)>damageSpell){
				//Debug.LogError("okay1");
				ChiffreBarriere.Text = "-"+damageSpell;
				damageSpell = 0;
				ChiffreBarriere.gameObject.SetActiveRecursively(true);
				StartCoroutine(ScaleText(ChiffreBarriere));
			}
			else{
				//Debug.LogError("okay2");
				damageSpell = damageSpell - int.Parse(ChiffreBarriere.Text);
				//ChiffreBarriere.Text= "+"+(int.Parse(ChiffreBarriere.Text)+damageSpell);
				ChiffreBarriere.Text= "-"+ChiffreBarriere.Text;
				ChiffreBarriere.gameObject.SetActiveRecursively(true);
				StartCoroutine(ScaleText(ChiffreBarriere));
			}
		}
		
		//TODO: Add esquive voleur
		//
		if(PersoReceveur.currentSpell !=null){
			if(PersoReceveur.currentSpell.Type == "Contre" && PersoReceveur.Id!=3){
				//Ecrir quelque chose de différent en fonction du type de perso
				ChiffreDommage.Text = "-"+damageSpell+" +10";
			}else{
				if(damageSpell<=0)
					ChiffreDommage.Text = " ";
				else
					ChiffreDommage.Text = "-"+damageSpell;
			}
		}
		else{
			if(damageSpell<=0)
				ChiffreDommage.Text = " ";
			else
				ChiffreDommage.Text = "-"+damageSpell;
		}
		
		HudVie.Text = PersoReceveur.Pv.ToString();
		HudBarriere.Text = PersoReceveur.Shield.ToString();
		HudRessource.Text = PersoAttaquant.NbRessource.ToString();
		
		
		
		if(PersoReceveur.currentSpell.Type == "Contre" && (PersoAttaquant.currentSpell.IdSpell == 101 || PersoAttaquant.currentSpell.IdSpell ==201 || PersoAttaquant.currentSpell.IdSpell ==301)){
			
			if(ZoneFlash.name == "FlashJ1"){
				ZoneFlash=this.FlashJ2;
			}else{
				ZoneFlash=this.FlashJ1;
			}
			
			ActionContrePerso(spell,PersoReceveur,PersoAttaquant,ChiffreDommage,HudVie,HudRessource);
			ChiffreDommage.gameObject.SetActiveRecursively(true);
			StartCoroutine(ScaleText(ChiffreDommage));
			StartCoroutine(EffectRessource(spell.Gain - spell.Cost,GainPerteRessource));
			ZoneFlash.material.color = Color.yellow;
			yield return StartCoroutine(this.flashHud(ZoneFlash));
			ZoneFlash.material.color = Color.white;
			
		}
		else{
			ChiffreDommage.gameObject.SetActiveRecursively(true);
			StartCoroutine(ScaleText(ChiffreDommage));
			StartCoroutine(EffectRessource(spell.Gain - spell.Cost,GainPerteRessource));
			yield return StartCoroutine(this.flashHud(ZoneFlash));
		}
		
		yield return new WaitForSeconds(1f);//on atten que sa scale et on reprend
		//ImageIcone.gameObject.SetActiveRecursively(false);
		ChiffreDommage.gameObject.SetActiveRecursively(false);
		ChiffreDommage.transform.localScale = new Vector3(0.45f,0.45f,0.45f);
		ChiffreDommage.Text = " ";
		ChiffreBarriere.gameObject.SetActiveRecursively(false);
		ChiffreBarriere.transform.localScale = new Vector3(0.45f,0.45f,0.45f);
		ChiffreBarriere.Text = " ";
	}
	public void ActionContrePerso(Spells spell, Personnage PersoReceveur,Personnage PersoAttaquant, CCText ChiffreDommage,CCText HudVie,CCText HudRessource){
		
		if(PersoReceveur.Id==1)//Warrior inflige 10 dommage
		{
			if(ChiffreDommage.name == "PerteVieJ1"){
				ChiffreDommage=this.PerteVieJ2;
			}else{
				ChiffreDommage=this.PerteVieJ1;
			}
			if(HudVie.name == "ChiffreVieJ1"){
				HudVie=this.ChiffreVieJ2;
			}else{
				HudVie=this.ChiffreVieJ1;
			}
			ChiffreDommage.Text = "-10";
			ChiffreDommage.gameObject.SetActiveRecursively(true);
			StartCoroutine(ScaleText(ChiffreDommage));
			HudVie.Text = PersoAttaquant.Pv.ToString();
		}else if(PersoReceveur.Id==2){//Wizard met barriere de 10
			CCText ChiffreGainPerteBarriere;
			CCText HudBarriere;
			if(ChiffreDommage.name == "PerteVieJ1"){
				ChiffreGainPerteBarriere=this.GainPerteBarriereJ2;
				HudBarriere = this.ChiffreBarriereJ1;
			}else{
				ChiffreGainPerteBarriere=this.GainPerteBarriereJ1;
				HudBarriere = this.ChiffreBarriereJ2;
			}
			GainPerteBarriereJ1.Text = "+10";
			GainPerteBarriereJ1.gameObject.SetActiveRecursively(true);
			StartCoroutine(ScaleText(GainPerteBarriereJ1));
			HudBarriere.Text = PersoReceveur.Shield.ToString();
		}else if(PersoReceveur.Id==3 && spell.IdSpell !=108 && spell.IdSpell !=207 && spell.IdSpell !=308){//Thief
			ChiffreDommage.Text = "0 esq";
			ChiffreDommage.gameObject.SetActiveRecursively(true);
			StartCoroutine(ScaleText(ChiffreDommage));
		}
	}
	public IEnumerator EffectBoubou(Spells spell, MeshRenderer ImageIcone,CCText ChiffreGainBarriere, MeshRenderer ZoneFlash,CCText HudBarriere, CCText HudRessource, CCText GainPerteRessource,Personnage Perso){
		//Heal de 20 en dure pas encore la donnée en xml
		//
		ImageIcone.gameObject.SetActiveRecursively(true);
		ImageIcone.material.mainTexture = spell.Image;
		
		ChiffreGainBarriere.Text = "+25";
		HudRessource.Text = Perso.NbRessource.ToString();
		HudBarriere.Text = Perso.Shield.ToString();
		ChiffreGainBarriere.gameObject.SetActiveRecursively(true);
		
		StartCoroutine(ScaleText(ChiffreGainBarriere));//on atten que sa scale et on reprend
		StartCoroutine(EffectRessource(spell.Gain - spell.Cost,GainPerteRessource));
		
		ZoneFlash.material.color = Color.blue;
		yield return StartCoroutine(this.flashHud(ZoneFlash));
		ZoneFlash.material.color = Color.white;
		yield return new WaitForSeconds(1f);
		ChiffreGainBarriere.gameObject.SetActiveRecursively(false);
		//ImageIcone.gameObject.SetActiveRecursively(false);
		ChiffreGainBarriere.transform.localScale = new Vector3(0.45f,0.45f,0.45f);
	}
	public IEnumerator EffectHeal(Spells spell, MeshRenderer ImageIcone,CCText ChiffreGainVie, MeshRenderer ZoneFlash,CCText HudVie, CCText HudRessource, CCText GainPerteRessource,Personnage Perso){
		//Heal de 20 en dure pas encore la donnée en xml
		//
		ImageIcone.gameObject.SetActiveRecursively(true);
		ImageIcone.material.mainTexture = spell.Image;
		ChiffreGainVie.Text = "+20";
		HudVie.Text = Perso.Pv.ToString();
		HudRessource.Text = Perso.NbRessource.ToString();
		ChiffreGainVie.gameObject.SetActiveRecursively(true);
		StartCoroutine(ScaleText(ChiffreGainVie));//on atten que sa scale et on reprend
		StartCoroutine(EffectRessource(spell.Gain - spell.Cost,GainPerteRessource));
		
		ZoneFlash.material.color = Color.green;
		yield return StartCoroutine(this.flashHud(ZoneFlash));
		ZoneFlash.material.color = Color.white;
		yield return new WaitForSeconds(1f);
		ChiffreGainVie.gameObject.SetActiveRecursively(false);
		//ImageIcone.gameObject.SetActiveRecursively(false);
		ChiffreGainVie.transform.localScale = new Vector3(0.45f,0.45f,0.45f);
	}
	public IEnumerator EffectRessource(int spellcost, CCText ChiffreRessource){
		ChiffreRessource.Text = "";
		if(spellcost<0){
			ChiffreRessource.Text = ChiffreRessource.Text + spellcost.ToString();
		}
		else if(spellcost>0){
			ChiffreRessource.Text = "+";
			ChiffreRessource.Text = ChiffreRessource.Text + spellcost.ToString();
		}
		else{
			ChiffreRessource.Text = spellcost.ToString();
		}
		
		
		ChiffreRessource.gameObject.SetActiveRecursively(true);
		StartCoroutine(ScaleText(ChiffreRessource));//on atten que sa scale et on reprend
		yield return new WaitForSeconds(1f);
		
		ChiffreRessource.gameObject.SetActiveRecursively(false);
		ChiffreRessource.transform.localScale = new Vector3(0.45f,0.45f,0.45f);
	}
	public IEnumerator flashHud(MeshRenderer Flash){
		for(int i=0;i<5;i++){
			Flash.gameObject.SetActiveRecursively(true);
			yield return new WaitForSeconds(0.1f);
			Flash.gameObject.SetActiveRecursively(false);
			yield return new WaitForSeconds(0.1f);
		}
	}
	public IEnumerator ScaleText(CCText HudToScale){//0.45 a 1 et inversement
		float nombreScale=0.015f;
		HudToScale.transform.localScale = new Vector3(HudToScale.transform.localScale.x +nombreScale,HudToScale.transform.localScale.y  +nombreScale,HudToScale.transform.localScale.z  +nombreScale);
		yield return new WaitForSeconds(0.01f);
		if(HudToScale.transform.localScale.x<=1){
			StartCoroutine(ScaleText(HudToScale));
		}
		else{
			yield return new WaitForSeconds(0.5f);
			HudToScale.gameObject.SetActiveRecursively(false);
			HudToScale.transform.localScale = new Vector3(0.45f,0.45f,0.45f);
		}
	}
	public IEnumerator DamageDot(int DamageDot,Personnage PersoReceveur,CCText ChiffreGainPerteBarriere,CCText ChiffreBarriere,CCText ChiffreGainVie,CCText ChiffreVie,MeshRenderer Flash){
		//Le dot lancé marche bien mais on ne voit pas l'energie baisser ==> TODO
		//
		yield return new WaitForSeconds(0f);
		if(DamageDot>0){
			if(PersoReceveur.Shield>0){
				if(int.Parse(ChiffreBarriere.Text)>DamageDot){
					ChiffreBarriere.Text = "-"+DamageDot;
					DamageDot = 0;
					ChiffreBarriere.gameObject.SetActiveRecursively(true);
					StartCoroutine(ScaleText(ChiffreBarriere));
				}
				else{
					DamageDot = DamageDot - int.Parse(ChiffreBarriere.Text);
					ChiffreBarriere.Text= "-"+ChiffreBarriere.Text;
					ChiffreBarriere.gameObject.SetActiveRecursively(true);
					StartCoroutine(ScaleText(ChiffreBarriere));
				}
			}
			if(DamageDot<=0){
				ChiffreGainVie.Text = " ";
			}else{
				ChiffreGainVie.Text = "-"+DamageDot;
				
			}
			ChiffreGainVie.gameObject.SetActiveRecursively(true);
			StartCoroutine(ScaleText(ChiffreGainVie));//on atten que sa scale et on reprend
			
			ChiffreVie.Text = PersoReceveur.Pv.ToString();
			ChiffreBarriere.Text = PersoReceveur.Shield.ToString();
				
			Flash.material.color = Color.red;
			yield return StartCoroutine(this.flashHud(Flash));
			Flash.material.color = Color.white;
			yield return new WaitForSeconds(1f);
			ChiffreGainVie.gameObject.SetActiveRecursively(false);
			ChiffreGainVie.transform.localScale = new Vector3(0.45f,0.45f,0.45f);
			ChiffreGainPerteBarriere.gameObject.SetActiveRecursively(false);
			ChiffreGainPerteBarriere.transform.localScale = new Vector3(0.45f,0.45f,0.45f);
		}
	}
}
